package com.shupian.controller;

import com.shupian.domain.ResponseResult;
import com.shupian.domain.dto.AddRoleDto;
import com.shupian.domain.dto.ChangeStatusDto;
import com.shupian.domain.dto.UpdateRoleDto;
import com.shupian.enums.AppHttpCodeEnum;
import com.shupian.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/role")
@Validated
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 分页查询角色列表
     * @param pageNum
     * @param pageSize
     * @param roleName
     * @param status
     * @return
     */
    @GetMapping("/list")
    public ResponseResult listPage(Integer pageNum,Integer pageSize,String roleName ,String status){
        return roleService.getAllRole(pageNum,pageSize,roleName,status);
    }

    /**
     * 修改角色状态
     * @param changeStatusDto
     * @return
     */
    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody ChangeStatusDto changeStatusDto){
        return roleService.changeStatus(changeStatusDto);
    }

    /**
     * 添加角色
     * @param addRoleDto
     * @return
     */
    @PostMapping
    public ResponseResult addRole(@RequestBody AddRoleDto addRoleDto){
        return roleService.addRole(addRoleDto);
    }


    @GetMapping("/{id}")
    public ResponseResult getRole(@PathVariable Long id){
        return roleService.getRole(id);
    }

    @PutMapping
    public ResponseResult updateRole(@RequestBody UpdateRoleDto updateRoleDto){
        return roleService.updateRole(updateRoleDto);
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteRole(@PathVariable("id") Long id){
        return roleService.removeById(id) ?
                ResponseResult.okResult() :
                ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }
}
